/**
 * 根目录的数据模型
 * @type {RoodCategory}
 */
/****************/
module.exports = class RoodCategory extends require('./model'){

  /**获取根目录*/
  static getRootCategory() {
    return new Promise((resolve,reject) => {
      let sql ='SELECT * FROM root_category';

      console.log('获取Root类目sql='+sql);
      console.log('------------------------');

      this.dbQuery(sql).then(results => {
        resolve(results);
      }).catch(error => {
        console.log(`获取Root类目失败：${ error.message }`)
        reject(error);
      })
    })
  }

  /**
   * 插入主类目
   * @param root_category 主类目信息
   * @returns {Promise<插入主类目>}
   */
  static setRootCategory(root_category) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO root_category SET ?';
      console.log('插入root类目sql=' + sql);
      console.log('name=' + root_category.name + ';describe=' + root_category.describe + ';--------------------------');

      this.dbQuery(sql, root_category)
        .then(result => {
          resolve(result.insertId);
        })
        .catch(error => {
          console.log('插入主类目失败！！！');
          reject(error);
        })
    })
  }

  /**
   * 删除主类目
   * @param id 主类目id
   * @returns {Promise<unknown>}
   */
  static deleteRootCategory(id) {
    return new Promise((resolve, reject) => {
      let sql = 'DELETE FROM root_category WHERE id = ?'

      console.log('删除root主类目sql=' + sql);
      console.log('id=' + id);

      this.dbQuery(sql, id)
        .then(result => {
          resolve(result.affectedRows);
        })
        .catch(error => {
          console.log('删除主类目失败！！！');
          reject(error);
        })
    })
  }

  /**
   * 更新主类目
   * @param root_category
   * @returns {Promise<unknown>}
   */
  static updateRootCategory(root_category) {
    return new Promise((resolve, reject) => {
      let sql = `UPDATE root_category SET root_category.name = '${root_category.name}', 
        root_category.describe = '${root_category.describe}' WHERE root_category.id = ${root_category.id}`

      console.log('更新根类目的sql=' + sql);
      console.log('name=' + root_category.name + '; describe=' + root_category.describe + '--------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result.affectedRows)
        })
        .catch(error => {
          console.log('更新根目录方法失败！！！');
          reject(error)
        })
    })
  }

}